package com.itheima.inputstreamreader;

import java.io.*;

public class InputDemo1 {

    public static void main(String[] args) {
        // 代码: UTF-8 文件: GBK
        try (
                Reader reader = new FileReader("./day11-promotion-app/src/a.txt")
                )
        {
            char[] buffer = new char[3];
            int len = 0;
            while ((len = reader.read(buffer)) != -1) {
                System.out.print(new String(buffer, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 此时输出乱码
        System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");


        // 创建一个 InputStreamReader 对象， 其先获取文件的原始字节流，再将其按照真实的字符集转换为字符输入流，这样字符输入流中的字符就不乱码了。
        try (
                InputStreamReader isr = new InputStreamReader(new FileInputStream("./day11-promotion-app/src/a.txt"), "GBK");
                // 创建缓冲输入流对象，将字符输入流包装成字符缓冲输入流对象
                BufferedReader br = new BufferedReader(isr);
                )
        {
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
